package servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import entity.Area;
import entity.City;
import entity.Province;
import util.JDBCUtil;
import net.sf.json.JSONArray;

@WebServlet("/area")
public class AreaServlet extends HttpServlet {
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String method = req.getParameter("method");
		switch (method) {
		case "selectProvince":
			selectProvince(req, resp);
			break;
		case "selectCity":
			selectCity(req, resp);
			break;
		case "selectArea":
			selectArea(req, resp);
			break;
		default:
			break;
		}
	}

	private void selectArea(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String cityId = req.getParameter("cityId");

		ArrayList<Area> list = new ArrayList<Area>();
		Connection connection = null;
		PreparedStatement statement = null;
		ResultSet resultSet = null;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "SELECT id,area FROM tm_area where city_id=?";
			statement = connection.prepareStatement(sql);
			statement.setInt(1, Integer.parseInt(cityId));
			resultSet = statement.executeQuery();
			while (resultSet.next()) {
				int id = resultSet.getInt("id");
				String areaName = resultSet.getString("area");
				Area area = new Area(id, areaName);
				list.add(area);
			}

			for (Area area : list) {
				System.out.println(area);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, statement, resultSet);
		}

		// 以json格式返回List<Province>
		JSONArray jsonArray = JSONArray.fromObject(list);
		resp.setContentType("text/html;charset=utf-8");
		resp.getWriter().write(jsonArray.toString());
	}

	private void selectCity(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String provinceId = req.getParameter("provinceId");

		ArrayList<City> list = new ArrayList<City>();
		Connection connection = null;
		PreparedStatement statement = null;
		ResultSet resultSet = null;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "SELECT id,city FROM tm_city where province_id=?";
			statement = connection.prepareStatement(sql);
			statement.setInt(1, Integer.parseInt(provinceId));
			resultSet = statement.executeQuery();
			while (resultSet.next()) {
				int id = resultSet.getInt("id");
				String cityName = resultSet.getString("city");
				City city = new City(id, cityName);
				list.add(city);
			}

			for (City city : list) {
				System.out.println(city);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, statement, resultSet);
		}

		// 以json格式返回List<Province>
		JSONArray jsonArray = JSONArray.fromObject(list);
		resp.setContentType("text/html;charset=utf-8");
		resp.getWriter().write(jsonArray.toString());

	}

	private void selectProvince(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		ArrayList<Province> list = new ArrayList<Province>();
		Connection connection = null;
		Statement statement = null;
		ResultSet resultSet = null;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "SELECT id,province FROM tm_province";
			statement = connection.createStatement();
			resultSet = statement.executeQuery(sql);
			while (resultSet.next()) {
				int id = resultSet.getInt("id");
				String provinceName = resultSet.getString("province");
				Province province = new Province(id, provinceName);
				list.add(province);
			}

			for (Province province : list) {
				System.out.println(province);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, statement, resultSet);
		}

		// 以json格式返回List<Province>
		JSONArray jsonArray = JSONArray.fromObject(list);
		resp.setContentType("text/html;charset=utf-8");
		resp.getWriter().write(jsonArray.toString());
	}
}
